<template>
  <div id="sms-list">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb">
        <li class="breadcrumb-item ms-2">
          <router-link to="/tool/sms">短信</router-link>
        </li>
        <li class="ms-auto me-2" @click.stop="back()">返回</li>
      </ol>
    </nav>
    <div class="card">
      <div class="card-body py-1">
        <form class="row">
          <div class="col-1">
            <input
              class="form-control"
              placeholder="手机号"
              size="11"
              v-model.trim="mobile"
            />
          </div>
          <div class="col-1">
            <input
              class="form-control ms-2"
              placeholder="内容"
              size="10"
              v-model.trim="remark"
            />
          </div>

          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary btn-sm ms-1"
              @click.stop="reset()"
            >
              重置
            </button>

            <button
              type="button"
              class="btn btn-success me-1 ms-auto float-end"
              @click.stop="newSms()"
            >
              发送
            </button>
          </div>
        </form>
      </div>
    </div>
    <sms-detail-list :dataList="dataList"></sms-detail-list>

    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-sms ref="modalSms">
      <span slot="title">{{ modalTitle }}</span>
    </my-modal-sms>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination.vue";
import SmsDetailList from "@/components/list/sms-detail-list.vue";
import MyModalSms from "@/components/modal/my-modal-sms_v2";
import { searchSms, sendFlightOrderSms } from "@/api/sms";

export default {
  components: {
    MyPagination,
    SmsDetailList,
    MyModalSms,
  },
  data() {
    return {
      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 25,
        pageTotal: 0,
      },
      mobile: "",
      remark: "",

      modalTitle: "",
    };
  },
  mounted: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      this.$router.replace("/h5/tools/sms");
    } else {
      this.search();
    }
  },
  methods: {
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      this.showLoading();
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.mobile": this.mobile,
        "sc.remark": this.remark,
      };
      searchSms(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    newSms: function () {
      this.modalTitle = "编码导入发送短信";
      this.$refs.modalSms
        .modal()
        .then((v) => {
          // console.log(v.mobile, v.smsContent)
          sendFlightOrderSms(
            this.id,
            {
              psgId: psgInfo.id,
              mobileNo: v.mobile,
              smsContent: v.smsContent,
              smsFormat: v.smsFormat,
            },
            (v) => {
              this.commonShowMessage(v);
            }
          );
        })
        .catch((ex) => {});
    },
    reset: function () {
      this.mobile = "";
      this.remark = "";
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
